/*
 * @Description: 
 * @Author: mmzhang
 * @Date: 2021-12-10 10:13:45
 * @LastEditTime: 2022-08-16 16:26:01
 */
import Opt from '@/components/dt/info/Opt.js'
import businessCommonMgr from '@/biz/businessReport/businessCommon'
import dateOpt from '@/biz/option/date.js'
import ruleOpt from '@/biz/option/rule.js'
import tool from '@/common/tool.js'

let _this = null
let companys = []
let developer = []
class SearchOpt extends Opt {

  constructor(vm) {
    _this = vm
    super()
    this.build()
  }

  getCompany() {
    let opt = {
      title: '区域筛选',
      key: 'companyIds',
      placeholder: '全部',
      tree: {
        options: companys,
      }
    }
    let params = {
      isTwoLevel: true,
    }
    businessCommonMgr
      .getOrganizationTree(params)
      .then((res) => {
        companys = res
        opt.tree.options = companys
      })
    return opt
  }

  getDeveloper(value = '', flag) {
    let opt = {
      title: '开发商简称',
      key: 'developerAbbreviations',
      placeholder: '全部',
      tree: {
        options: developer,
        searchBtn: true,
        searchFun: (val) => {
          this.getDeveloper(val, 1)
        }
      }
    }

    let formData = new FormData()
    formData.append('name', value)

    businessCommonMgr
      .getListDeveloperAbbreviation(formData)
      .then((res) => {
        developer = res
        if (!flag) {
          opt.tree.options = developer
        } else {
          this.all[3].tree.options = developer
        }
      })
    return opt
  }

  changeProduct() {
    let arr = businessCommonMgr.getProductType()
    let typeList = JSON.parse(JSON.stringify(arr))

    if (_this.formValidate.productTypes.length) {
      typeList.forEach((item) => {
        item.disabled = true
        if (_this.formValidate.productTypes.indexOf(item.key) > -1) {
          item.disabled = false
        }
      })
    } else {
      typeList.forEach((item) => {
        item.disabled = false
      })
    }

    _this.formValidate.quantityTypes = _this.formValidate.productTypes.length ?
      _this.formValidate.productTypes :
      businessCommonMgr.roomType()
    _this.formValidate.areaTypes = _this.formValidate.productTypes
    _this.formValidate.amountTypes = _this.formValidate.productTypes

    this.all[6].select.options = typeList
    this.all[7].select.options = typeList
    this.all[8].select.options = typeList
  }


  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    let hideList = [
    {
      title: '产品类型',
      placeholder: '全部',
      key: 'productTypes',
      xl: 5,
      select: {
        options: businessCommonMgr.getProductType(),
        multiple: true,
        maxTagCount: 1
      },
      change: (val) => {
        this.changeProduct()
      }
    }, {
      title: '套数取值类型',
      placeholder: '全部',
      key: 'quantityTypes',
      xl: 4,
      select: {
        options: businessCommonMgr.getProductType(),
        multiple: true,
        maxTagCount: 1
      }
    }, {
      title: '面积取值类型',
      placeholder: '全部',
      key: 'areaTypes',
      xl: 5,
      select: {
        options: businessCommonMgr.getProductType(),
        multiple: true,
        maxTagCount: 1
      }
    }, {
      title: '金额取值类型',
      placeholder: '全部',
      key: 'amountTypes',
      xl: 5,
      select: {
        options: businessCommonMgr.getProductType(),
        multiple: true,
        maxTagCount: 1
      }
    }]
    let showList = [
      this.getCompany(),
      {
        key: 'checkTime',
        xl: 4,
        dtTime: {},
        rules: [
          ruleOpt.timeInterval(2),
          // ruleOpt.check('不支持选择单边日期', (value) => {
          //   return value[0] == '' ^ value[1] == ''
          // })
        ]
      },
      {
        title: '合作模式',
        placeholder: '全部',
        key: 'coopModes',
        select: {
          options: businessCommonMgr.getCooperationModeAttrSort(),
          multiple: true,
          maxTagCount: 1
        }
      },
      this.getDeveloper('', 0),
      {
        title: '',
        xl: 3,
        placeholder: '请输入案场名称搜索',
        key: 'buildingName',
        input: {}
      }
    ]
    if (_this.showFlag) {
      this.all = [
        ...showList,
        ...hideList
      ]
    } else {
      this.all = [...showList]
    }
  }
}

export default SearchOpt